package org.overmind.ieat.server.controllers.provider;

import org.overmind.ieat.server.entity.Provider;
import org.overmind.ieat.server.logic.provider.ProviderManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author eugene.karanda
 */
@RestController
@RequestMapping("/provider")
public class ProviderController {

    private static Logger logger = LoggerFactory.getLogger(ProviderController.class);

    @Autowired
    private ProviderManager providerManager;

    @RequestMapping(method = RequestMethod.GET)
    public List<Provider> getAll() {
        return providerManager.getProviderList();
    }

    @Secured("ROLE_ADMIN")
    @RequestMapping(value = "/{id:.+}", method = RequestMethod.GET)
    public Provider get(@PathVariable("id") int id) {
        return providerManager.findById(id);
    }

    @RequestMapping(value = "/{id:.+}", method = RequestMethod.DELETE)
    public boolean delete(@PathVariable("id") int id) {
        return providerManager.deleteById(id);
    }

    @RequestMapping(method = RequestMethod.POST)
    public Integer save(@RequestBody @Valid Provider provider) {
        return providerManager.save(provider);
    }

    @RequestMapping(value = "/{id:.+}", method = RequestMethod.PUT)
    public Provider update(@PathVariable("id") int id, @RequestBody @Valid Provider provider) {
        return providerManager.update(id, provider);
    }

}